package com.online.servlet.user;

import com.online.pojo.Bill;
import com.online.pojo.User;
import com.online.pojo.menu;
import com.online.service.user.Impl.UserServiceImpl;
import com.online.service.user.Impl.billServiceImpl;
import com.online.service.user.Impl.merchantServiceImpl;
import com.online.service.user.UserService;
import com.online.service.user.billService;
import com.online.service.user.merchantService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class payServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_name=req.getParameter("user_name");
        double account = Double.parseDouble(req.getParameter("account"));
        double price= Double.parseDouble(req.getParameter("price"));
        System.out.println("price="+price);System.out.println("account="+account);
        if (price<account){
            List<Bill> bill=new ArrayList<Bill>();
            billService billService=new billServiceImpl();
            try {
                billService.update(user_name);
                billService.select(bill,user_name);
                req.setAttribute("bill",bill);

                List<Bill> billed=new ArrayList<Bill>();
                billService.selectBilled(billed,user_name);
                req.setAttribute("billed",billed);
                UserService userService=new UserServiceImpl();
                List<menu> list=new ArrayList<menu>();
                userService.showAllmenu(list);
                req.setAttribute("m",list);
                System.out.println(bill);
                userService.cutM(user_name,price);
                User user= userService.login(user_name);
                merchantService merchantService=new merchantServiceImpl();
                int k=0;
                while ( k<bill.size() ){
                    merchantService.update(bill.get(k));
                }
                req.setAttribute("user_name",user.getUser_name());
                req.setAttribute("account",user.getAccount());
                req.getRequestDispatcher("/jsp/orderpage.jsp").forward(req,resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else{
            UserService userService=new UserServiceImpl();
            User user= userService.login(user_name);
            req.setAttribute("user_name", user.getUser_name());
            req.setAttribute("number",user.getUser_number());
            req.setAttribute("account",user.getAccount());
            req.setAttribute("recharge","余额不足，请充值！");
            req.getRequestDispatcher("/jsp/recharge.jsp").forward(req,resp);
        }
//        List<Bill> bill=new ArrayList<Bill>();
//        billService billService=new billServiceImpl();
//        try {
//            billService.update(user_name);
//            billService.select(bill,user_name);
//            req.setAttribute("bill",bill);
//
//            List<Bill> billed=new ArrayList<Bill>();
//            billService.selectBilled(billed,user_name);
//            req.setAttribute("billed",billed);
//            UserService userService=new UserServiceImpl();
//            User user= userService.login(user_name);
//            req.setAttribute("user_name",user.getUser_name());
//            req.setAttribute("account",user.getAccount());
//
//            List<menu> list=new ArrayList<menu>();
//            userService.showAllmenu(list);
//            req.setAttribute("m",list);
//            System.out.println(bill);
//            req.getRequestDispatcher("/jsp/orderpage.jsp").forward(req,resp);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
